<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="归属学科" prop="subject_id">
        <el-select
          v-model="formData.subject_id"
          placeholder="请选择学科"
          clearable
        >
          <el-option
            v-for="item in treeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="归属目录">
        <el-select
          v-model="formData.directory_id"
          placeholder="请选择目录"
          clearable
        >
          <el-option
            v-for="item in directoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="tag_name">
        <el-input v-model="formData.tag_name" placeholder="请输入" style="width:200px" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="formData.sort"
          :min="1"
          :max="100"
          :rows="3"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="is_show">
        <el-switch
          v-model="formData.is_show"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>

      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addTagListApi, updateTagApi } from '@/api/subject_tag'
export default {
  name: 'AddFormTag',
  data() {
    return {
      treeList: [],
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        is_show: '',
        sort: 10,
        tag_name: '',
        subject_id: '',
        directory_id: ''
      },
      // 表单验证规则
      formRules: {
        is_show: [{ required: true, message: '请输入', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入', trigger: 'blur' }],
        subject_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        directory_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        tag_name: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    },
    directoryList() { // 目录信息
      if (!this.formData.subject_id) {
        return []
      } else {
        const result = this.treeList.find(val => val.id === this.formData.subject_id)
        return result.children
      }
    }

  },
  methods: {

    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateTagApi(this.formData)
        : await addTagListApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        is_show: '',
        sort: 10,
        tag_name: '',
        subject_id: '',
        directory_id: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style scoped></style>
